package com.diy.sigmund.diyuser.test.controller;

import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.spring.anno.SecurityParameter;
import com.diy.sigmund.diycommon.test.entity.MqTestVO;
import com.diy.sigmund.diycommon.util.json.JacksonUtil;
import com.diy.sigmund.diyuser.test.entity.SecurityParameterDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ylm-sigmund
 * @since 2021/8/17 16:31
 */
@RestController
@RequestMapping("/securityParameter")
public class SecurityParameterController {

    /**
     * AES,DES加解密测试ok
     * {
     * "userId": "110",
     * "name": "小明",
     * "age": 1,
     * "phoneNum": "demoData",
     * "idCard": "demoData",
     * "createTime": "2021-08-17 16:41:00"
     * }
     *
     * @param securityParameterDto userDto
     * @return "tyQEctOtnGIGQT6z5ssarOnId6YA+8/m2XnVhhjRGZWr3/X1PjIc1jXq758/H2+e3BJDYqMWSMoJppjl4z1Jy/9x6Gp4w2cXPKoeU0FAYgf0ICNTnuDTJDxBl4qfrzRXiUO4ChPX4Zt4TnFhvwZEPnoO3aRLYskiXRU5c1oT2t0VnYdDK/4I0HoO3aRLYskiXRU5c1oT2t2d6+99+EwWexewdcxulvBL+ihRs4eNdiMtJBmzafPCynoO3aRLYskiBZ6EptomcZqU0s50jueb2A=="
     */
    @SecurityParameter(inDecode = false)
    @PostMapping("/encode")
    public ResultMsg encode(@RequestBody SecurityParameterDTO securityParameterDto) {
        final MqTestVO mqTestVo = new MqTestVO();
        mqTestVo.setUserId(securityParameterDto.getUserId());
        mqTestVo.setName(securityParameterDto.getName());
        return ResultMsg.success(mqTestVo);
    }

    /**
     * @param dto ResultMsg的加密串
     * @return ResultMsg
     */
    @SecurityParameter(outEncode = false)
    @PostMapping("/decode")
    public ResultMsg decode(@RequestBody ResultMsg dto) {
        final SecurityParameterDTO securityParameterDto = JacksonUtil.toObject(JacksonUtil.toJson(dto.getData()),
                SecurityParameterDTO.class);
        final MqTestVO mqTestVo = new MqTestVO();
        mqTestVo.setUserId(securityParameterDto.getUserId());
        mqTestVo.setName(securityParameterDto.getName());
        return ResultMsg.success(mqTestVo);
    }

    /**
     * AES加解密测试ok
     *
     * @param dto ResultMsg的加密串
     * @return ResultMsg
     */
    @SecurityParameter
    @PostMapping("/decodeEncode")
    public ResultMsg decodeEncode(@RequestBody ResultMsg dto) {
        final SecurityParameterDTO securityParameterDto = JacksonUtil.toObject(JacksonUtil.toJson(dto.getData()),
                SecurityParameterDTO.class);
        final MqTestVO mqTestVo = new MqTestVO();
        mqTestVo.setUserId(securityParameterDto.getUserId());
        mqTestVo.setName(securityParameterDto.getName());
        return ResultMsg.success(mqTestVo);
    }

}
